$(function(){
    let _deleteIds = null, _action = '/add',_row = null;
    const convertor = {};
    const alerter = Frame2.alert.init({$div:$('#alertbar')});
    const table = $('#table').bootstrapTable({
        url: _DIR + '/table',
        method: 'POST',
        toolbar:'#formbar',
        pagination: true,
        sidePagination: 'server',
        pageNumber: 1,
        pageSize: 15,
        pageList: [15, 20, 50, 100],
        columns: [{checkbox: true, valign: "bottom"},
        {
            showSelectTitle:true,
            field: 'id',
            title: 'ID'
        }, {
            field: 'type',
            title: '类别'
        }, {
            field: 'val',
            title: '字段值'
        }, {
            field: 'name',
            title: '回显值'
        }, {
            field: 'sorting',
            title: '排序(升序)'
        }, {
            field: 'description',
            title: '备注 用途'
        }, {
            title:'操作',
            align: 'center',
            class: 'action',
            width: 210,
            formatter: (value, row, index, field) => {
                let btn = '<button type="button" class="btn btn-xs btn-success view" title="点击查看转换详情">查看</button>'
                     + '<button type="button" class="btn btn-xs btn-primary update">修改</button>'
                     +'<button type="button" class="btn btn-xs btn-danger del">删除</button>';
                return btn;
            },
            events:{
                'click .view': function(e,value,row,index){
                    let view = $('#viewModal');
                    view.find('#view_type').text(row.type);
                    view.find('#view_val').text(row.val);
                    view.find('#view_name').text(row.name);
                    view.find('#view_sorting').text(row.sorting);
                    view.find('#view_createTime').text(new Date(row.createTime).Format('yyyy年MM月dd日 hh:mm:ss'));
                    view.find('#view_updateTime').text(new Date(row.updateTime).Format('yyyy年MM月dd日 hh:mm:ss'));
                    view.find('#view_description').text(row.description);

                    view.modal('show');
                },
                'click .update': function(e,value,row,index) {
                    let form = $('#addForm'),modal = $('#addModal');
                    _action = '/update';
                    modal.find('.modal-title').text('修改转换规则');
                    form.find("[name='id']").val(row.id);
                    form.find("[name='type']").val(row.type);
                    form.find("[name='val']").val(row.val);
                    form.find("[name='name']").val(row.name);
                    form.find("[name='sorting']").val(row.sorting);
                    form.find("[name='description']").val(row.description);

                    modal.modal('show');
                    form.data('bootstrapValidator').resetForm();
                    //alert(form.data('bootstrapValidator').isValid())
                },
                'click .del': function(e,value,row,index){
                    _deleteIds = [row.id];
                    $('#deleteModal').modal('show');
                },
            }
        }],
        queryParams:function(params) {
            let $array = $('#formbar').serializeArray();
            $.each($array, function(index, val) {
                if(val.value && val.value.trim())
                    params[val.name]=val.value.trim();
            });
            return params;
        },
    });

    $('#listbar').on('click', '.add', function(event) {
        event.preventDefault();
        let form = $('#addForm'),modal = $('#addModal');
        form.data('bootstrapValidator').resetForm();
        form.get(0).reset();
        _action = '/add';
        modal.find('.modal-title').text('创建新转换规则');
        modal.modal('show');
    });
    $('#listbar').on('click', '.del', function(event) {
        event.preventDefault();
        _deleteIds = Frame2.bootstrap.getSelectionsAttr(table,'id');
        if (_deleteIds == null || _deleteIds.length == 0) {
            alerter.show(100,function(){},{autohide:3000,title:'提示：',content:'请选择要删除的数据！'})
        } else{
            $('#deleteModal').modal('show');
        };
    });
    //确认删除事件
    $('#deleteModal').on('click', '.del', function(event) {
        event.preventDefault();
        console.log(_deleteIds)
        $.ajax({
            url: _DIR + '/delete',
            type: 'POST',
            dataType: 'json',
            traditional:true,
            data: {ids: _deleteIds},
            success: function(result) {
                $('#deleteModal').modal('hide');
                if (result.code == 200) {
                    alerter.show(100,function(){},{autohide:3000,content:'数据删除成功。'})
                    table.bootstrapTable('refresh');
                } else{
                    alerter.show(100,function(){},{autohide:5000,content:result.message})
                };
            },complete: function(){
                _deleteIds = [];
            }
        });
    });
    //新增表单验证+提交
    $('#addForm').bootstrapValidator(Frame2.bootstrap.validatorConfig({
        fields:{
            type: {
                message: '类别不合法',//默认提示信息
                validators: {
                    notEmpty: {
                        message: '类别不能为空'
                    },
                    stringLength: {
                        min: 2,
                        max: 32,
                        message: '类别只能是2-32个字符'
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9_\.]+$/,
                        message: '开关编码(ID)只能包含大写、小写字母、数字、下划线和英文点'
                    }
                }
            },
            val: {
                message: '转换值不合法',//默认提示信息
                validators: {
                    notEmpty: {
                        message: '转换值不能为空'
                    },
                    stringLength: {
                        min: 1,
                        max: 10,
                        message: '转换值只能是1-10个字符'
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9]+$/,
                        message: '转换值只能包含大写、小写字母、数字'
                    }
                }
            },
            name: {
                message: '回显名称不合法',//默认提示信息
                validators: {
                    notEmpty: {
                        message: '回显名称不能为空'
                    },
                    stringLength: {
                        min: 1,
                        max: 20,
                        message: '回显名称必须是1-20个字符'
                    }
                }
            },
            sorting: {
                message: '请设置转换值显示顺序',
                validators: {
                    notEmpty: {
                        message: '转换值顺序不能为空'
                    },
                    number: {
                        min : 1,
                        max : 9999,
                        message: '转换值顺序只能是1-9999'
                    }
                }
            },
        }
    })).on('success.form.bv', function(e) {
        e.preventDefault();
        let form = $(e.target);
        $.ajax({
            url: _DIR + _action,
            type: 'POST',
            dataType: 'json',
            data: form.serialize(),
            success: function(result) {
                if (result.code == 200) {
                    alerter.show(100,function(){},{autohide:3000,content:'操作成功。'})
                    $('#addModal').modal('hide');
                    table.bootstrapTable('refresh');
                } else{
                    alerter.show(100,function(){},{autohide:5000,content:result.message})
                };
            }
        });
        return false;
    });

});
